package com.doradance.app.mine;


import android.content.Context;
import android.os.Bundle;
import android.print.PrintJob;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.ShareUtil;
import com.doradance.app.appactivity.AddBabyActivity;
import com.doradance.app.appactivity.message.MessageDetailActivity;
import com.doradance.app.home.VipRechargeActivity;
import com.doradance.app.mine.collect.CollectActivity;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.AddBabyEntity;
import com.doradance.entity.EventModifyPhone;
import com.doradance.entity.HideHomeRedEntity;
import com.doradance.entity.HideRedEntity;
import com.doradance.entity.MineModifyEntity;
import com.doradance.entity.RefreActivityEntity;
import com.doradance.entity.UserHomeEntity;
import com.doradance.entity.refreshEntity;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.dialog.IOSAlertListFragment;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {

    @Bind(R.id.civ_mine)
    CircleImageView mCivMine;
    @Bind(R.id.iv_mine_icon)
    CircleImageView mIvMineIcon;
    @Bind(R.id.tv_mine_name)
    TextView mTvMineName;
    String avatar;
    String nick_name;
    String phone;
    @Bind(R.id.iv_add1)
    CircleImageView mIvAdd1;
    @Bind(R.id.tv_name_add1)
    TextView mTvNameAdd1;
    @Bind(R.id.tv_birthday_add1)
    TextView mTvBirthdayAdd1;
    @Bind(R.id.iv_add2)
    CircleImageView mIvAdd2;
    @Bind(R.id.tv_name_add2)
    TextView mTvNameAdd2;
    @Bind(R.id.tv_birthday_add2)
    TextView mTvBirthdayAdd2;
    @Bind(R.id.iv_add3)
    CircleImageView mIvAdd3;
    @Bind(R.id.tv_name_add3)
    TextView mTvNameAdd3;
    @Bind(R.id.tv_birthday_add3)
    TextView mTvBirthdayAdd3;
    @Bind(R.id.ll_add1)
    LinearLayout mLlAdd1;
    @Bind(R.id.ll_add2)
    LinearLayout mLlAdd2;
    @Bind(R.id.ll_add3)
    LinearLayout mLlAdd3;
    UserHomeEntity o1;
    String[] actionArr;
    String initSelectId;
    //弹起dialog
    public final static int MINEADD = 1;
    public final static int MINEMODIFY = 5;
    @Bind(R.id.iv_bg_1)
    ImageView mIvBg1;
    @Bind(R.id.iv_bg_2)
    ImageView mIvBg2;
    @Bind(R.id.iv_bg_3)
    ImageView mIvBg3;
    @Bind(R.id.iv_mine_red)
    ImageView mIvMineRed;

    public MineFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initData();
        EventBus.getDefault().register(this);
        return rootView;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void initData() {
        MyHttpRequest request = new MyHttpRequest(getActivity());
        request.get(UrlCenter.USERS_HOME, null, new DataRequestListener<UserHomeEntity>(UserHomeEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(UserHomeEntity o) {
                super.onSuccess(o);
                o1 = o;
                initMineInfo(o);
            }
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    private void initMineInfo(final UserHomeEntity o) {
        avatar = o.getAvatar();
        nick_name = o.getNick_name();
        phone = o.getPhone();
        initAdd();
        mCivMine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditPersionActivity.start(getActivity(), o.getAvatar(), mTvMineName.getText().toString(), phone);
            }
        });
        if (!TextUtils.isEmpty(o.getAvatar())) {
            Glide.with(getActivity()).load(o.getAvatar()).dontAnimate().placeholder(R.mipmap.init_logo).into(mCivMine);
        } else {
            Glide.with(getActivity()).load(R.mipmap.init_logo).into(mCivMine);
        }
        if (!TextUtils.isEmpty(o.getNick_name())) {
            mTvMineName.setText(o.getNick_name());
        } else {
            mTvMineName.setText("尚未设置昵称");
        }
        Glide.with(getActivity()).load(o.getVip_icon()).into(mIvMineIcon);
        mLlAdd1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddBabyActivity.start(getActivity(), MINEADD);
            }
        });
        mLlAdd2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddBabyActivity.start(getActivity(), MINEADD);
            }
        });
        mLlAdd3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddBabyActivity.start(getActivity(), MINEADD);
            }
        });

        initSelectId = ShareUtil.getBabyId();
        if (o.getBabies().size() == 1) {
            initAdd1(o.getBabies().get(0).getId(), o.getBabies().get(0).getAvatar(), o.getBabies().get(0).getNick_name(), o.getBabies().get(0).getAge_label());

        } else if (o.getBabies().size() == 2) {
            initAdd1(o.getBabies().get(0).getId(), o.getBabies().get(0).getAvatar(), o.getBabies().get(0).getNick_name(), o.getBabies().get(0).getAge_label());
            initAdd2(o.getBabies().get(1).getId(), o.getBabies().get(1).getAvatar(), o.getBabies().get(1).getNick_name(), o.getBabies().get(1).getAge_label());
        } else {
            initAdd1(o.getBabies().get(0).getId(), o.getBabies().get(0).getAvatar(), o.getBabies().get(0).getNick_name(), o.getBabies().get(0).getAge_label());
            initAdd2(o.getBabies().get(1).getId(), o.getBabies().get(1).getAvatar(), o.getBabies().get(1).getNick_name(), o.getBabies().get(1).getAge_label());
            initAdd3(o.getBabies().get(2).getId(), o.getBabies().get(2).getAvatar(), o.getBabies().get(2).getNick_name(), o.getBabies().get(2).getAge_label());
        }
    }

    private void initAdd() {
        Glide.with(getActivity()).load(R.mipmap.icon_mine_add).into(mIvAdd1);
        mTvNameAdd1.setText("");
        mTvBirthdayAdd1.setText("");

        Glide.with(getActivity()).load(R.mipmap.icon_mine_add).into(mIvAdd2);
        mTvNameAdd2.setText("");
        mTvBirthdayAdd2.setText("");

        Glide.with(getActivity()).load(R.mipmap.icon_mine_add).into(mIvAdd3);
        mTvNameAdd3.setText("");
        mTvBirthdayAdd3.setText("");

    }


    private void initAdd1(final int babyId, String avatar, String nick_name, String age_label) {
        Glide.with(getActivity()).load(avatar).into(mIvAdd1);
        mTvNameAdd1.setText(nick_name);
        mTvBirthdayAdd1.setText(age_label);
        Log.e("nitSelectId","nitSelectId"+initSelectId+"babyId"+babyId);
        if (initSelectId.equals(babyId + "")) {
            Glide.with(getActivity()).load(R.mipmap.icon_selected_bg).into(mIvBg1);
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg2);
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg3);
        } else {
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg1);
        }
        mLlAdd1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSelectBaby(babyId, 0);
            }
        });
    }


    private void initAdd2(final int babyId, String avatar, String nick_name, String age_label) {
        Glide.with(getActivity()).load(avatar).into(mIvAdd2);
        mTvNameAdd2.setText(nick_name);
        mTvBirthdayAdd2.setText(age_label);
        if (initSelectId.equals(babyId + "")) {
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg1);
            Glide.with(getActivity()).load(R.mipmap.icon_selected_bg).into(mIvBg2);
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg3);

        } else {
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg2);
        }
        mLlAdd2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSelectBaby(babyId, 1);
            }
        });
    }

    private void initAdd3(final int babyId, String avatar, String nick_name, String age_label) {
        Glide.with(getActivity()).load(avatar).into(mIvAdd3);
        mTvNameAdd3.setText(nick_name);
        mTvBirthdayAdd3.setText(age_label);
        if (initSelectId.equals(babyId + "")) {
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg1);
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg2);
            Glide.with(getActivity()).load(R.mipmap.icon_selected_bg).into(mIvBg3);
        } else {
            Glide.with(getActivity()).load(R.mipmap.icon_mine_bg).into(mIvBg3);
        }
        mLlAdd3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSelectBaby(babyId, 2);
            }
        });
    }

    private void isSelectBaby(int babyId, final int num) {
        String selectId = ShareUtil.getBabyId();
        if (selectId.equals(babyId + "")) {
            actionArr = new String[]{"编辑宝宝信息"};
        } else {
            actionArr = new String[]{"编辑宝宝信息", "设为当前宝宝"};
        }
        final IOSAlertListFragment diaFrag = new IOSAlertListFragment();
        diaFrag.setActionList(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        UserHomeEntity.BabiesBean babiesBean = o1.getBabies().get(num);
                        AddBabyActivity.start(getActivity(), babiesBean, MINEMODIFY);
                        diaFrag.dismiss();
                        break;
                    case 1:
                        //选择的宝宝保存在本地
                        UserHomeEntity.BabiesBean babiesBean1 = o1.getBabies().get(num);
                        ShareUtil.setBabyId(babiesBean1.getId() + "");
                        ShareUtil.setBabyName(babiesBean1.getNick_name());
                        initMineInfo(o1);
                        EasyToast.showToast(getContext(), "切换宝宝成功");
                        diaFrag.dismiss();
                        break;
                }
            }
        }, actionArr).show(getFragmentManager());
//
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(MineModifyEntity message) {
        if (!TextUtils.isEmpty(message.avater)) {
            Glide.with(getActivity()).load(message.avater).into(mCivMine);
            avatar = message.avater;
        }
        if (!TextUtils.isEmpty(message.nickname)) {
            mTvMineName.setText(message.nickname);
            nick_name = message.nickname;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventModifyPhone refreshEvent) {
        phone = refreshEvent.phone;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(AddBabyEntity refreshEvent) {
        o1 = refreshEvent.o;
        initMineInfo(refreshEvent.o);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(refreshEntity refreshEvent){
        mIvMineRed.setVisibility(View.VISIBLE);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HideRedEntity refreshEvent){
        mIvMineRed.setVisibility(View.GONE);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick({R.id.ll_vip, R.id.ll_video, R.id.ll_collection, R.id.ll_help, R.id.ll_set, R.id.civ_mine, R.id.iv_message})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_vip:
                VipRechargeActivity.start(getActivity(),-1+"");
                break;
            case R.id.ll_video:
                StartVideoActivity.start(getActivity());
                break;
            case R.id.ll_collection:
                CollectActivity.start(getActivity());
                break;
            case R.id.ll_help:
                HelpActivity.start(getActivity());
                break;
            case R.id.ll_set:
                SetActivity.start(getActivity());
                break;
            case R.id.iv_message:
                MessageDetailActivity.start(getActivity());
                mIvMineRed.setVisibility(View.GONE);
                EventBus.getDefault().post(new HideHomeRedEntity());
                break;
        }
    }
}
